//
//  MDHumidifierController.m
//  MideaWorld_Zizhu
//
//  Created by Minus on 15/8/25.
//  Copyright (c) 2015年 Minus. All rights reserved.
//

#import "MDHumidifierController.h"
#import "MDXPGApplianceApi.h"
#import "MDUserDefaults.h"
#import "Humidification.h"
#import "IDataBodyHumiStatus.h"
#import "DataBodyHumiRequest.h"
#import <Availability.h>

@interface MDHumidifierController ()
{
    DataBodyHumiRequest *cmdData;
    IOSByteArray *order;
}


@property (weak, nonatomic) IBOutlet UISwitch *powerSwitch;
@property (weak, nonatomic) IBOutlet UISegmentedControl *humiditySegment;
@property (weak, nonatomic) IBOutlet UISegmentedControl *windSpeedSegment;
@property (weak, nonatomic) IBOutlet UISwitch *lightSwitch;
@property (weak, nonatomic) IBOutlet UITextView *resultTextView;

@end

@implementation MDHumidifierController

- (void)viewDidLoad
{
    [super viewDidLoad];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

- (IBAction)actionQuery:(id)sender
{
    FactoryDataBody *query = (FactoryDataBody *)[FactoryDataBody queryRequestWithInt:FactoryDataBody_get_Humidifier()];
    order = [query toBytes];
    
    self.resultTextView.text = @"正在发送命令...";
    
    [[MDXPGApplianceApi getInstance] transparentCmd:[MDUserDefaults sharedInstance].currentApplianceID order:order completionForHumidifier:^(BaseMessage *baseMessage, NSString *reply, Humidification *status) {
        
        if ([baseMessage getCode] == 0)
        {
            [self.resultTextView insertText:@"命令发送成功..."];
            [self.resultTextView insertText:[status description]];
        }
        else
        {
            [self.resultTextView insertText:@"命令发送失败..."];
            [self.resultTextView insertText:[baseMessage getMessage]];
        }
    }];
}

- (IBAction)actionSendCmd:(id)sender
{
    Humidification *humi = [Humidification new];
    
    // 开关
    humi->powerMode_ = self.powerSwitch.on ? 1 : 0;
    
    // 加湿量
    switch (self.humiditySegment.selectedSegmentIndex)
    {
        case 0:
            humi->humidity_set_ = IDataBodyHumiStatus_get_HumiLevel1_();    // 40%
            break;
            
        case 1:
            humi->humidity_set_ = IDataBodyHumiStatus_get_HumiLevel2_();    // 50%
            break;
            
        case 2:
            humi->humidity_set_ = IDataBodyHumiStatus_get_HumiLevel3_();    // 60%
            break;
            
        case 3:
            humi->humidity_set_ = IDataBodyHumiStatus_get_HumiLevel4_();    // F%
            break;
            
        default:
            break;
    }
    
    // 风速
    switch (self.windSpeedSegment.selectedSegmentIndex)
    {
        case 0:
            humi->windSpeed_ = IDataBodyHumiStatus_get_WIND_SPEED_LOW();  // 低风
            break;
            
        case 1:
            humi->windSpeed_ = IDataBodyHumiStatus_get_WIND_SPEED_MIDDLE();  // 中风
            break;
            
        case 2:
            humi->windSpeed_ = IDataBodyHumiStatus_get_WIND_SPEED_HIGH();  // 高风
            break;
            
        case 3:
            humi->windSpeed_ = IDataBodyHumiStatus_get_WIND_SPEED_AUTO(); // 自动风
            break;
            
        default:
            break;
    }
    
    // 灯光
    humi->isDisplay_ = self.lightSwitch.on;
    
    cmdData = (DataBodyHumiRequest *)[FactoryDataBody cmdRequestWithInt:FactoryDataBody_get_Humidifier()];
    [cmdData setDataBodyStatusWithBaseDevice:humi];
    order = [cmdData toBytes];
    
    self.resultTextView.text = @"正在发送命令...";
    
    [[MDXPGApplianceApi getInstance] transparentCmd:[MDUserDefaults sharedInstance].currentApplianceID order:order completionForHumidifier:^(BaseMessage *baseMessage, NSString *reply, Humidification *status) {
        
        if ([baseMessage getCode] == 0)
        {
            [self.resultTextView insertText:@"命令发送成功..."];
            [self.resultTextView insertText:[status description]];
        }
        else
        {
            [self.resultTextView insertText:@"命令发送失败..."];
            [self.resultTextView insertText:[baseMessage getMessage]];
        }
    }];
}

@end
